package com.graduation_design.modules.admin.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.graduation_design.common.lang.Result;
import com.graduation_design.entity.AppProduct;
import com.graduation_design.modules.app.controller.BaseController;
import com.graduation_design.modules.search.config.SearchRabbitConfig;
import com.graduation_design.service.AppCategoryService;
import com.graduation_design.service.AppProductService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

@RestController
@RequestMapping("/admin/product")
public class AdminProductController extends BaseController {

    @Resource
    AppCategoryService appCategoryService;

    @Resource
    AppProductService appProductService;

    @Resource
    RabbitTemplate rabbitTemplate;

    @GetMapping("/list")
//    @RequiresPermissions("admin:product:list")
    public Result list(String name, Long categoryId) {
        return Result.success(MapUtil.builder()
                .put("categories", appCategoryService.list())
                .put("pageData", appProductService.pageWithCategory(getPage(), name, categoryId))
                .build()
        );
    }

    @GetMapping("/info/{id}")
//    @RequiresPermissions("admin:product:list")
    public Result info(@PathVariable Long id) {
        return Result.success(appProductService.getWithAttrsById(id));
    }

    @PostMapping("/save")
//    @RequiresPermissions(value = {"admin:product:save", "admin:product:update"}, logical = Logical.OR)
    public Result save(@Validated @RequestBody AppProduct appProduct) {

        appProductService.create(appProduct);

        // 添加es搜索
        rabbitTemplate.convertAndSend(SearchRabbitConfig.SEARCH_EXCHANGE, SearchRabbitConfig.PRODUCT_SEARCH_CREATE_KEY, appProduct.getId());
        return Result.success(appProduct);
    }

    @PostMapping("/delete")
//    @RequiresPermissions("admin:product:delete")
    public Result delete(@RequestBody Long[] ids) {

        appProductService.removeByIds(Arrays.asList(ids));

        // 删除es搜索
        rabbitTemplate.convertAndSend(SearchRabbitConfig.SEARCH_EXCHANGE, SearchRabbitConfig.PRODUCT_SEARCH_DELETE_KEY, Arrays.asList(ids));
        return Result.success();
    }

    @PostMapping("/updateState")
//    @RequiresPermissions("admin:product:updateState")
    public Result updateState(long id, Boolean isOnSale, Boolean isNew, Boolean isTop, Boolean isHot) {

        appProductService.update(new UpdateWrapper<AppProduct>()
                .set(isOnSale != null, "is_on_sale", isOnSale)
                .set(isNew != null, "is_new", isNew)
                .set(isTop != null, "is_top", isTop)
                .set(isHot != null, "is_hot", isHot)
                .eq("id", id)
        );

        if (isOnSale != null) {
            rabbitTemplate.convertAndSend(SearchRabbitConfig.SEARCH_EXCHANGE, SearchRabbitConfig.PRODUCT_SEARCH_CREATE_KEY, id);
        }

        return Result.success();
    }

    /**
     *分开写
     */
//    @PostMapping("/updateIsOnSale")
//    public Result updateIsOnSale(@RequestParam(required = true) Long id,
//                                 @RequestParam(required = true) boolean isOnSale) {
//
//        appProductService.update(new UpdateWrapper<AppProduct>()
//                .set("is_on_sale", isOnSale)
//                .eq("id", id)
//        );
//
//        // 添加es搜索
//        rabbitTemplate.convertAndSend(SearchRabbitConfig.SEARCH_EXCHANGE, SearchRabbitConfig.PRODUCT_SEARCH_CREATE_KEY, id);
//        return Result.success();
//    }
//
//    @PostMapping("/updateIsHot")
//    public Result updateIsHot(@RequestParam(required = true) Long id,
//                              @RequestParam(required = true) boolean isHot) {
//
//        appProductService.update(new UpdateWrapper<AppProduct>()
//                .set("is_hot", isHot)
//                .eq("id", id)
//        );
//        return Result.success();
//    }
//
//    @PostMapping("/updateIsTop")
//    public Result updateIsTop(@RequestParam(required = true) Long id,
//                              @RequestParam(required = true) boolean isTop) {
//
//        appProductService.update(new UpdateWrapper<AppProduct>()
//                .set("is_top", isTop)
//                .eq("id", id)
//        );
//        return Result.success();
//    }
//
//    @PostMapping("/updateIsNew")
//    public Result updateIsNew(@RequestParam(required = true) Long id,
//                              @RequestParam(required = true) boolean isNew) {
//
//        appProductService.update(new UpdateWrapper<AppProduct>()
//                .set("is_new", isNew)
//                .eq("id", id)
//        );
//        return Result.success();
//    }

}
